﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SqlToMongoCore.Properties;

namespace SqlToMongoCore
{
    class Program
    {
        static StringBuilder logBuilder = new StringBuilder();
        static void Main(string[] args)
        {
            if (Settings.Default.IsDebug)
            {
                Console.WriteLine("It is in debug mode. Please attach process and press any key to continue.");
                Console.ReadKey();
            }

            ConsoleWriteLineContent("-----  SQL to Mongo  -----");
            ConsoleWriteLine();
            ConsoleWriteLog("Program starts.");


            if (args == null)
            {
                args = new string[] { };
            }

            PreExecute(args);

            Console.WriteLine("Press 's' to save log and open it; or any other key to exit.");

            var keyPress = Console.ReadKey();
            if (keyPress.KeyChar == 's' || keyPress.KeyChar == 'S')
            {
                string fileName = Path.GetTempFileName() + ".log";
                File.WriteAllText(fileName, logBuilder.ToString());
                Process.Start(fileName);
            }
        }

        private static void PreExecute(string[] args)
        {
            ConsoleWriteLog("Initializing configuration information...");

            XDocument xDocument = null;
            if (args.Length >= 0)
            {
                try
                {
                    xDocument = XDocument.Parse(args[0].DecodeBase64());
                }
                catch (Exception ex)
                {
                    ConsoleWriteLog("Failed to decode configuration information.");
                    return;
                }
            }
            else
            {
                try
                {
                    xDocument = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration.xml"));
                }
                catch (Exception ex)
                {
                    ConsoleWriteLog("Failed to load default configuration information.");
                    return;
                }
            }

            ConfigurationOperator configurationOperator = new ConfigurationOperator(xDocument);
            Execute(configurationOperator);
        }

        private static void Execute(ConfigurationOperator configurationOperator)
        {
            Translator translator = new Translator(configurationOperator.TranslatorSettings);
            translator.Translate();
        }

        /// <summary>
        /// Consoles the write log.
        /// </summary>
        /// <param name="content">The content.</param>
        public static void ConsoleWriteLog(string content)
        {
            ConsoleWriteLineContent(string.Format("{0}: {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), content.DBToString()));
        }

        /// <summary>
        /// Consoles the write line.
        /// </summary>
        public static void ConsoleWriteLine()
        {
            ConsoleWriteLineContent("-----------------------------------");
        }

        /// <summary>
        /// Consoles the content of the write line.
        /// </summary>
        /// <param name="content">The content.</param>
        public static void ConsoleWriteLineContent(string content)
        {
            Console.WriteLine(content);
            logBuilder.Append(content + Environment.NewLine);
        }
    }
}
